/**
 * Created by Administrator on 15-6-26.
 */
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有产品...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		getProv : function(id){
			var prov = "";
			var p = PROVINCES;
			if(!id) return prov;
			for(var i in p){
				var areaid = p[i]["area_id"];
				if(areaid==id){
					prov = p[i]["area_name"];
					break;
				}
			}
			return prov;
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	},
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	getData : function(){
		return this.statics.oData;
	},
	setData : function(id,data){
		if(id && data){
			this.statics.oData[id] = data;
		}else{
			this.statics.oData = {};
		}
	},
	onPrevNextClick : function(that,e){
		
		var tarBtn = $(e.currentTarget);
		if(tarBtn.hasClass("disable")) return false;
		var oData = that.statics.oData;
		var curPage = that.statics.currentPage;
		//var last_mid = oData[curPage]["last"];
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		if(tarBtn.hasClass("prevBtn")){

			that.nextBtn.removeClass("disable");
			that.statics.currentPage = toPage;
			//console.log(oData[toPage]);
			that.buildHtml(oData[toPage]);
			if(!that.statics.oData[toPage-1]) that.prevBtn.addClass("disable");
		}else{
			that.prevBtn.removeClass("disable");
			
			if(oData[toPage]){
				that.statics.currentPage = toPage;
				that.buildHtml(oData[toPage]);
				if(!that.statics.oData[toPage+1] && !that.statics.last){
					that.nextBtn.addClass("disable");
				}
			}else{
				that.fire("query_nexta",oData[curPage]["last"]);
			}
		}
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 8;
		var last = opt.last || "";
		var provice = opt.provice || "";
		//console.log(provice)
		var city = opt.city || "";
		//console.log(city)
		var title = opt.title || "";
		//console.log(title)
		var supplier = opt.supplier || "";
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data = {
			pageSize : pageSize,
			last : last,
			city : city,
			provice : provice,
			title : title,
			supplier : supplier,
			act : "lists",
			currentPage : last,
			set_page : "1"
		};
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		PFT.Ajax({
			url : "channels_conf.html",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage += 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			//console.log(res)
			var code = res.status;
			var lists = res.lists;
			var last = res.last;
			//alert(code);
			if(code=="success"){
				if(!that.statics.isObjEmpty(lists)){
					that.statics.oData[that.statics.currentPage] = res;
					//console.log(that.statics.oData[that.statics.currentPage])
					//alert("200");
					that.buildHtml(res);
				}else{
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
			}else if(code==0){
				listUl.html(status["unlogin"]);
			}else{
				listUl.html(status["fail"]);
			}
		})
	},
	buildHtml : function(res){
		var that = this;
		var code = res.code;
		var lists = res.lists;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage;
		//console.log(total)
		//console.log(last)
		str += '<div class="tbbox">';
			str += '		<table class="tb_scenic" border="0" cellpadding="0" cellspacing="0">';
			str += '			<thead>';
			str += '			  <tr>';
			str += '				<th class="setalign">票类</th>';
			str += '				<th class="supplier">供应商</th>';
			str += '				<th class="setwidth1"></th>';
			//str += '				<th class="setwidth">推荐级别</th>';
			str += '				<th class="setwidth"></th>';
			str += '				<th class="pay_sale">销售渠道</th> ';
			str += '			  </tr>';
			str += '			</thead>';
		for(var i in data){
			is_data_empty = false;
			str += '	<tbody class="check_body">';
			str += ' 		<tr class="setbg">';
			str += '			<td colspan="3" class="setalign setcolor_b"><input class="check_set sethe" data-y="0"name="" data-set="'+i+'" type="checkbox" value="'+i+'" />'+data[i]["title"]+'</td>';
			//str += '			<td  class="secl"><span class="setcolor_a">3</span> <input type="text" class="select_text"  value=""><span class="setcolor_b btn_edit">更改</span><span class="setcolor_save save_edit" id="save_edit">保存</span></td>';
			str += '			<td  class="secl"><span class="setcolor_a"></span> <input type="text" class="select_text"  value=""><span class="setcolor_b btn_edit"></span><span class="setcolor_save save_edit" id="save_edit"></span></td>';
			str += '			<td class="secl_bottom"></td>';
			str += '		</tr>';
			for(var s in data[i]["list"]){
				var lists =data[i]["list"][s];
				var pid = data[i]["list"][s]["pid"];
				var lid = data[i]["list"][s]["lid"];
				var aid = data[i]["list"][s]["aid"];
				var dname = data[i]["list"][s]["dname"];
				var tt = data[i]["list"][s]["title"];
				var channel = data[i]["list"][s]["channel"];
				str += '<tr class="set_all_data">';
				str += '<input type="hidden" class="pida_" value="">';
				str += '<input type="hidden" class="aida_" value="">';
				str += '<input type="hidden" class="lida_" value="">';
				str += '<input type="hidden" name="" class="aid_" value="">';
				str += '<input type="hidden" name="" class="pid_" value="">';
				str += '<input type="hidden" name="" class="lid_" value="">';
				str += '<input type="hidden"  class="allid" data-pid="'+pid+'" data-aid="'+aid+'" data-lid="'+lid+'">';
				str += '<td class="setalign setpadding" title="'+tt+'"><input class="set_check sethe" name="" type="checkbox"  data-pid="'+pid+'" data-aid="'+aid+'" data-lid="'+lid+'" data-x="0" data_test="" data-check="'+lid+'" value="'+lid+'" />'+tt+'</td>';
				str += '<td class="suppl" title="'+dname+'">'+dname+'</td>';
				str += '<td></td>';
				str += '<td></td>';
				str += '<td class="setalign_a "><span class="annel">'+channel+'</span><span class="btna_edit btn_choose" data-aid="'+aid+'"  data-lid="'+lid+'" data-pid="'+pid+'">更改</span></td>';
				str += '</tr>';  
			}
			str += '</tbody>';
		}
		str += '</table>';
		str += '	</div>';
		
		$("#mlistUl").html(str);
		$("#total_num").text("共"+totalPage+"页"); 
		$("#whichPageNum").text(that.statics.currentPage);
		// for(var i in lists){
			// var item = lists[i];
			// var title = lists[i];
			// var tickets = item.ticket;
			// //var area = item.area;
			// //console.log(tickets);
			// var ticketHtml = "";
			// var item_all = "";
			// var result = "";
			// var lists_div = "";
			// //var lists_con = ""; 
			// var itema = "";
			// var itemb = "";
			// //console.log(itemHtml)
			// for(var s in tickets){
				// //var tic = tickets[s];
				// var lists_ui =tickets[s]["lists"];
				// var lists_con = "";
				// //tic["ppid"] = tic["pid"];
				// //tic["lid_pid"] = s;
				// //item["lid_pid"] = i;
				// //title["title"] = item["title"];
				// var title_li = item["title"];
				// //tic["landname"] = tic["title"];
				// //var tictitle = tic["title"];
				// //lists_div += that.parseTemplate(item_div,tic);
				// //item["ticket_con"] = lists_div;
				// for(var j in lists_ui){
					// var lists_li = lists_ui[j];
					// lists_li["activea"] = lists_li["active"];
					// lists_li["aida"] = lists_li["aid"];
					// lists_li["dnamea"] = lists_li["dname"];
					// lists_li["jsa"] = lists_li["js"];
					// lists_li["lsa"] = lists_li["ls"];
					// lists_li["lvla"] = lists_li["lvl"];
					// lists_con += that.parseTemplate(item_con,lists_li);
					// //console.log(lists_li["ls"]);
				// }
				// //itema["ticket_con"] = lists_con;
				// //itema["landname"] = tictitle;
				// //itema["lid_pid"] = i;
				// //result += that.parseTemplate(item_div,itema);
				// //item_all += that.parseTemplate(item_a,title);
			// }
			// //itemb["tickets"] = result;
			// //itemb["title"] = title_li;
			// //build_div += that.parseTemplate(item_a,itemb);
		// }
		 //listUl.html(build_div);
		 //listUl.html(lists_con);
		//$("#whichPageNum").text(that.statics.currentPage);
	}
	
	
	
	
	
	
});





